public class BinaryTree {

    //使用内部类创建节点对象
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //创建二叉树
    public TreeNode createTree() {

        //实例化节点对象
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        //返回二叉树的根节点
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) return;

        System.out.print(root.val + " ");

        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) return;

        inOrder(root.left);

        System.out.print(root.val + " ");

        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) return;

        postOrder(root.left);
        postOrder(root.right);

        System.out.print(root.val + " ");
    }

    public int nodeSize;

    /**
     * 获取当前二叉树的节点个数
     * @param root
     * @return
     */
    public void getNodeSize(TreeNode root) {
        if (root == null) return;

        nodeSize++;

        getNodeSize(root.left);
        getNodeSize(root.right);
    }

    //子问题思路
    public int getNodeSize2(TreeNode root) {
        if (root == null) return 0;

        //左树的节点加上右树的节点加1
        return getNodeSize2(root.left) + getNodeSize2(root.right) + 1;
    }

    public int leafCount;

    /**
     * 获取该二叉树叶子节点的个数 —— 遍历思路
     * @param root
     */
    public void getLeafCount(TreeNode root) {
        if (root == null) return;

        if (root.left == null && root.right == null) {
            leafCount++;
        }

        getLeafCount(root.left);
        getLeafCount(root.right);
    }

    /**
     * 获取该二叉树叶子节点的个数 —— 子问题思路
     * @param root
     */
    public int getLeafCount2(TreeNode root) {
        if (root == null) return 0;

        if (root.left == null && root.right == null) {
            return 1;
        }

        return getLeafCount2(root.left) + getLeafCount2(root.right);
    }

    /**
     * 获取二叉树第K层的节点数
     * @param root
     * @return
     */
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }

        if (k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left, k - 1)
                + getKLevelNodeCount(root.right, k - 1);
    }

    /**
     * 求二叉树的高度
     * 时间复杂度为O(n)
     * @param root
     * @return
     */
    public int getHeight(TreeNode root) {
        if (root == null) return 0;

        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        //return Math.max(leftHeight, rightHeight) + 1;
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }

    /**
     * 寻找二叉树的节点值
     * @param root
     * @param key
     * @return
     */
    public TreeNode find(TreeNode root, int key) {
        if (root == null) return null;

        if (root.val == key) {
            return root;
        }

        TreeNode leftResult = find(root.left, key);
        if (leftResult != null) {
            return leftResult;
        }

        TreeNode rightResult = find(root.right, key);
        if (rightResult != null) {
            return rightResult;
        }

        return null;
    }

    /**
     * 判断两棵二叉树是否相同
     * 时间复杂度：p -> m, q -> n, O(min(m, n))
     * @param p
     * @param q
     * @return
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //1.结构上：一个为空，一个不为空
        if ((p == null && q != null) || (p != null && q == null)) {
            return false;
        }

        //2。此时：都不为空或者都为空
        if (p == null && q == null) {
            return true;
        }

        if (p.val != q.val) {
            return false;
        }

        //3.此时：p != null && q != null && p.val == q.val
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
